package com.jxw.cloudpen.web.util;

import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author ligang
 * @create 2025/7/22 10:58
 */
public class FileToByte {
    public static byte[] readPCMFile(String filePath) throws UnsupportedAudioFileException, IOException {
        File file = new File(filePath);
//        AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
//        AudioFormat format = audioStream.getFormat();
//
//        // 检查是否为PCM格式
//        if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
//            throw new IllegalArgumentException("The audio format is not PCM.");
//        }

        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytesBuffer = new byte[4096]; // 缓冲区大小可以根据需要调整
        int bytesRead;
        while ((bytesRead = fileInputStream.read(bytesBuffer)) != -1) {
            out.write(bytesBuffer, 0, bytesRead);
        }
        return out.toByteArray();
    }
    public static ByteString convertToByteString(byte[] pcmData) {
        return ByteString.copyFrom(pcmData);
    }

    public static void byteToFile(byte[] byteArray){
        // 字节数组，即byte string
        String filePath = "D:\\file.txt"; // 替换为你的文件路径
        try {
            // 写入文件
            Files.write(Paths.get(filePath), byteArray);
            System.out.println("文件写入成功！");
        } catch (IOException e) {
            System.err.println("写入文件时发生错误：" + e.getMessage());
        }
    }

    public static void byteToFile(String string){
        // 字节数组，即byte string
        String filePath = "D:\\file.txt"; // 替换为你的文件路径
        try {
            // 写入文件
            Files.write(Paths.get(filePath), string.getBytes(StandardCharsets.UTF_8));
            System.out.println("文件写入成功！");
        } catch (IOException e) {
            System.err.println("写入文件时发生错误：" + e.getMessage());
        }
    }



    public static void main(String[] args) {
        String path = "D:\\1+1.pcm";
        try {
            byte[] bytes = readPCMFile(path);
            String s = new String(bytes, StandardCharsets.UTF_8);
//            System.out.println(s);
            ByteString byteString = convertToByteString(readPCMFile(path));
            System.out.println(byteString);
//            byteToFile(readPCMFile(path));
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
